#include <assert.h>
#include "smartpictext.h"
#include "graphics.h"
#include "../../../core/command.h"

using namespace Commands;
using namespace Commands::Core;

SmartPicText::SmartPicText(int x, int y, int layer, int id, TFont* font)
: text(new TPicText(x, y, layer, id, font))
, justify(TOP_LEFT)
{}
//-----------------------------------------------------------------------------
SmartPicText::~SmartPicText()
{
    delete text;
}
//-----------------------------------------------------------------------------
void SmartPicText::SetValue(const int value)
{
    text->SetVal(value);
}
//-----------------------------------------------------------------------------
void SmartPicText::SetValue(const float value)
{
    text->SetVal(int(value + 0.5f));
}
//-----------------------------------------------------------------------------
SmartPicText::SmartPicText()
: text(0l)
{}
//-----------------------------------------------------------------------------
void SmartPicText::SetJustify(const Justify& justify)
{
    MGL_textJustType h_justify = MGL_LEFT_TEXT;
    MGL_textJustType v_justify = MGL_TOP_TEXT;
    switch(justify)
    {
        case TOP_LEFT:
            h_justify = MGL_LEFT_TEXT;
            v_justify = MGL_TOP_TEXT;
            break;
        case TOP_CENTER:
            h_justify = MGL_CENTER_TEXT;
            v_justify = MGL_TOP_TEXT;
            break;
        case TOP_RIGHT:
            h_justify = MGL_RIGHT_TEXT;
            v_justify = MGL_TOP_TEXT;
            break;
        case MIDDLE_LEFT:
            h_justify = MGL_LEFT_TEXT;
            v_justify = MGL_CENTER_TEXT;
            break;
        case MIDDLE_CENTER:
            h_justify = MGL_CENTER_TEXT;
            v_justify = MGL_CENTER_TEXT;
            break;
        case MIDDLE_RIGHT:
            h_justify = MGL_RIGHT_TEXT;
            v_justify = MGL_CENTER_TEXT;
            break;
        case BOTTOM_LEFT:
            h_justify = MGL_LEFT_TEXT;
            v_justify = MGL_BOTTOM_TEXT;
            break;
        case BOTTOM_CENTER:
            h_justify = MGL_CENTER_TEXT;
            v_justify = MGL_BOTTOM_TEXT;
            break;
        case BOTTOM_RIGHT:
            h_justify = MGL_RIGHT_TEXT;
            v_justify = MGL_BOTTOM_TEXT;
            break;
    }

    text->SetHJustify(h_justify);
    text->SetVJustify(v_justify);
}
//-----------------------------------------------------------------------------
float SmartPicText::GetWidth(void) const
{
    printf("UNREALISED METHOD\n");
    assert(false);
    return 0;
}
//-----------------------------------------------------------------------------
float SmartPicText::GetHeight(void) const
{
    printf("UNREALISED METHOD\n");
    assert(false);
    return 0;
}
//-----------------------------------------------------------------------------
void SmartPicText::SetX(const float x)
{
    AbstractPicture::SetX(x);
    text->SetX(x);
}
//-----------------------------------------------------------------------------
void SmartPicText::SetY(const float y)
{
    AbstractPicture::SetY(y);
    text->SetY(y);
}
//-----------------------------------------------------------------------------
void SmartPicText::SetXY(const float x, const float y)
{
    AbstractPicture::SetXY(x,y);
    text->SetXY(x, y);
}
//-----------------------------------------------------------------------------
void SmartPicText::SetAngle(const float angle)
{
    AbstractPicture::SetAngle(angle);
    text->SetAngle(angle);
}
//-----------------------------------------------------------------------------
void SmartPicText::SetXScale(const float scale)
{
    AbstractPicture::SetXScale(scale);
    text->SetXScale(scale);
}
//-----------------------------------------------------------------------------
void SmartPicText::SetYScale(const float scale)
{
    AbstractPicture::SetYScale(scale);
    text->SetYScale(scale);
}
//-----------------------------------------------------------------------------
void SmartPicText::SetScale(const float scale)
{
    AbstractPicture::SetScale(scale);
    text->SetXScale(scale);
    text->SetYScale(scale);
}
//-----------------------------------------------------------------------------
void SmartPicText::SetZOrder(const int order)
{
    AbstractPicture::SetZOrder(order);
    text->SetL(order / 50, order % 50);
}
//-----------------------------------------------------------------------------
void SmartPicText::SetFlip(const IGraphicElement::Flip flip)
{
    AbstractPicture::SetFlip(flip);
//    switch(flip)
//    {
//        case IGraphicElement::NONE:
//            text->SetOrientation(::Flip::NORMAL);
//        break;
//        case IGraphicElement::FLIPPED_HORIZONTAL:
//            text->SetOrientation(::Flip::FLIPPED_HORIZONTAL);
//        break;
//        case IGraphicElement::FLIPPED_VERTICAL:
//            text->SetOrientation(::Flip::FLIPPED_VERTICAL);
//        break;
//        case IGraphicElement::ROTATED_180:
//            text->SetOrientation(::Flip::ROTATED_180);
//        break;
//    }
}
//-----------------------------------------------------------------------------
void SmartPicText::SetAlpha(const float alpha)
{
    AbstractPicture::SetAlpha(alpha);
    text->SetAlpha(int32_t(alpha + 0.5));
}
//-----------------------------------------------------------------------------
void SmartPicText::Show(void)
{
    text->Out();
}
//-----------------------------------------------------------------------------
void SmartPicText::Hide(void)
{
    text->Hide();
}
//-----------------------------------------------------------------------------
void SmartPicText::SetXAnchor(const float anchor)
{
    AbstractPicture::SetXAnchor(anchor);
    text->SetXAnchor(anchor);
}
//-----------------------------------------------------------------------------
void SmartPicText::SetYAnchor(const float anchor)
{
    AbstractPicture::SetYAnchor(anchor);
    text->SetYAnchor(anchor);
}
//-----------------------------------------------------------------------------
void SmartPicText::AddCommand(const int queueId, const Command<IGraphicElement>& command)
{
    Command<IGraphicElement>* _command = command.Copy();
    _command->Attach(this);
    AddCommand(queueId, _command);
}
//-----------------------------------------------------------------------------
void SmartPicText::AddCommand(const int queueId, const Command<IText>& command)
{
    Command<Commands::IText>* _command = command.Copy();
    _command->Attach(this);
    AddCommand(queueId, _command);
}
//-----------------------------------------------------------------------------
